<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;
class Login  extends Controller
{
    public  function show($status,$message,$data=array()){
        $reuslt = array(
            'status'=>$status,
            'message'=>$message,
            'data'=>$data,
        );
        exit(json_encode($reuslt)) ;   //输出一条信息 并退出当前脚本
    }
    public function index()
    {
        return $this->fetch();
    }

    public function check(){
        $username = $_POST['username'];
        $password = $_POST['password'];
        if(!trim($username)){    //trim() 函数移除字符串两侧的空白字符或其他预定义字符
            return   $this->show(0,'账号不能为空');
        }
        if(!trim($password)){
            return   $this->show(0,'密码不能为空');
        }

        //验证码检验
        $captcha=input("captcha");
        //验证
        if (!captcha_check($captcha)){
            return $this->show(0,'验证码错误');
        }

        $map['username']=$username;

        $ret = Db::name('admin')->where($map)->find();


        if(!$ret){
            return $this->show(0,'该用户不存在');
        }

        if(($ret['password'])!=(md5($password))){
            return  $this->show(0,'密码错误');
        }


        Session::set('Admin',$ret);
        Cookie::set('loginName',$ret['username'],3600*24*10);

        return  $this->show(1,'登录成功');

    }
    public  function  loginout(){
        session('Admin',null);
        $this->redirect('index/index');
    }




}
